﻿/// <reference path="jquery.min.js" />
//#region juery 扩展
$.extend({
    isN: function (o) { return typeof o == "number" },
    isInt: function (o) { return $.isN(o) && Math.round(o) == o },
    round: function (n, mantissa) { if (!mantissa) mantissa = 0; if (mantissa <= 0) return Math.round(n); var v = 1; for (var i = 0; i < mantissa; i++) v *= 10; return Math.round(n * v) / v; },
    formatMoney: function (num, n) {
        num = String(num.toFixed(n ? n : 2));
        var re = /(-?\d+)(\d{3})/;
        while (re.test(num)) num = num.replace(re, "$1,$2")
        return n ? num : num.replace(/^([0-9,]+\.[1-9])0$/, "$1").replace(/^([0-9,]+)\.00$/, "$1");;
    },
});
//#endregion

var win = function () { };
win.prototype.MinMoney = 2000;//最低金额
win.prototype.MaxMoney = 3000000;//最高金额
win.prototype.FirmCapital = 0;//实盘资金
win.prototype.RiskMargin = 0;//风险保证金

win.prototype.Init = function () {
    var self = this;
    self.SlfTip = "最少2千，最多" + $.formatMoney(self.MaxMoney / 10000) + "万";

    var $li = $("#CapitalSelect").find("li");

    self.selectInit();

    $("#CapitalSelect").on("click", "li", function () {
        var $this = $(this);
        $li.removeClass("cur");
        $this.addClass("cur");
        self.FirmCapital = parseInt($this.attr("data"));
        if (self.FirmCapital < self.MinMoney) {
            self.FirmCapital = 0;
        }
        self.RiskMargin=self.getRiskMargin();
       
        self.moneyChange(self.FirmCapital);
        self.riskMarginChange(self.FirmCapital, self.RiskMargin);
        self.showLimit(self.FirmCapital);
        self.checkTodayShowable(self.FirmCapital);
    });
    $(":radio[name=RiskMargin]").bind("click", function () {
        self.RiskMargin = self.getRiskMargin();
        self.riskMarginChange(self.FirmCapital, self.RiskMargin);
        self.showLimit(self.FirmCapital, self.RiskMargin);
    });
    $("#InputFirmCapital").keypress(function (e) {
        var k = e.keyCode || e.which;
        if (k >= 48 && k <= 57 || k == 8) {
            return true;
        }
        return false;
    }).bind({
        "keyup": function () {
            var $this = $(this);
            var moneyStr = $.trim($this.val()).replace(/\,/g, "");
            if (!/^\d+$/.test(moneyStr)) {
                if (self.SlfTip != moneyStr) {
                    $this.val("");
                }
                return false;
            }

            var money = parseInt(moneyStr, 10);
            if (money > self.MaxMoney) {
                money = self.MaxMoney;
            }
            self.FirmCapital = money<self.MinMoney?0:money;
            self.RiskMargin = self.getRiskMargin();
            self.moneyChange(self.FirmCapital);
            self.riskMarginChange(self.FirmCapital, self.RiskMargin);
            self.showLimit(self.FirmCapital);
            self.checkTodayShowable(self.FirmCapital);
            $this.val($.formatMoney(money));
        }, "focusin": function () {
            var $this = $(this);
            $this.addClass("focusin").parent().addClass("focus");
            var moneyStr = $.trim($this.val()).replace(/\,/g, "");
            if (!/^\d+$/.test(moneyStr)) {
                $this.val("");
            }
        }, "focusout": function () {
            var $this = $(this);
            $this.parent().removeClass("focus");
            var moneyStr = $.trim($this.val()).replace(/\,/g, "");
            if (!/^\d+$/.test(moneyStr)) {
                $this.val(self.SlfTip);
                $this.removeClass("focusin");
            }
        }, "mouseover": function () {
            var $this = $(this);
            $this.parent().addClass("focus");
        }, "mouseout": function () {
            var $this = $(this);
            $this.parent().removeClass("focus");
        }
    });
    $("#FirmCapitalSelect").bind("click", function () {
        var $this = $(this);
        var cur = $this.attr("cur");
        var $FirmCapital = $("#FirmCapital");
        var $CapitalSelect = $("#CapitalSelect");
        if (cur == "select") {
            $this.attr("cur", "firminput");
            $this.html("选择实盘资金»");
            $FirmCapital.show();
            $CapitalSelect.hide();
        } else {
            $this.attr("cur", "select");
            $this.html("其他实盘资金»");
            $FirmCapital.hide();
            $CapitalSelect.show();
            self.selectInit();
        }
    })
};
win.prototype.selectInit = function () {
    var self = this;
    self.FirmCapital = parseInt($("#CapitalSelect").find("li.cur").attr("data"));
    self.RiskMargin = self.getRiskMargin();
    self.moneyChange(self.FirmCapital);
    self.riskMarginChange(self.FirmCapital, self.RiskMargin);
    self.showLimit(self.FirmCapital);
    self.checkTodayShowable(self.FirmCapital);
};
win.prototype.firminputInit = function () {
    var self = this;
    var moneyStr = $.trim($("#InputFirmCapital").val()).replace(/\,/g, "");
    if (!/^\d+$/.test(moneyStr)) {
        if (self.SlfTip != moneyStr) {
            $this.val("");
        }
        return false;
    }
};
//计算风险保证金
win.prototype.MathRiskMargin = function (money,lever) {
    if ($.isInt(money) && $.isInt(lever)) {
        if (lever == 5 || lever == 10 || lever == 15) {
            if (lever == 5) {
                return { warning: money * 110 / 100, open: money * 107 / 100, principal: Math.floor(money / lever), fee: $.round(money * 1.2 / 1000, 4), interest: 1.2 };
            } else if (lever == 10) {
                return { warning: money * 106 / 100, open: money * 104 / 100, principal: Math.floor(money / lever), fee: $.round(money * 1.5 / 1000, 4), interest: 1.5 };
            }
            return { warning: money * 105 / 100, open: money * 103 / 100, principal: Math.floor(money / lever), fee: $.round(money * 2 / 1000, 4), interest: 2 };
        }
    }
    return { warning: 0, open: 0, principal: 0, fee: 0, interest: 0 };
}
//当金额改变时
win.prototype.moneyChange = function (money) {
    if (!$.isInt(money)) return;
    var self = this;
    var riskMargin15 = self.MathRiskMargin(money, 15);
    var riskMargin10 = self.MathRiskMargin(money, 10);
    var riskMargin5 = self.MathRiskMargin(money, 5);

    var $riskMargin15 = $("#RiskMargin15");
    var $riskMargin10 = $("#RiskMargin10");
    var $riskMargin5 = $("#RiskMargin5");

    $riskMargin15.parent().show();
    $riskMargin10.parent().show();
    $riskMargin5.parent().show();

    $riskMargin15.next().text($.formatMoney(riskMargin15.principal));
    $riskMargin10.next().text($.formatMoney(riskMargin10.principal));
    $riskMargin5.next().text($.formatMoney(riskMargin5.principal));

    if (money >= 50000) {
        $riskMargin15.next().css("font-size", "16px");
        $riskMargin10.next().css("font-size", "16px");
        $riskMargin5.next().css("font-size", "16px");
    } else {
        $riskMargin15.next().css("font-size", "20px");
        $riskMargin10.next().css("font-size", "20px");
        $riskMargin5.next().css("font-size", "20px");
    }
    if (money > self.rm15) {
        $riskMargin15.parent().hide();
    }
    if (money > self.rm10) {
        $riskMargin10.parent().hide();
    }
};
win.prototype.riskMarginChange = function (money, rm) {
    var self = this;
    var info = self.MathRiskMargin(money, rm);
    if (info != null) {
        $("#TotalTrade").text(money > 0 ? $.formatMoney($.round(money + info.principal, 0)) : "0");
        $("#LossWarLine").text($.formatMoney($.round(info.warning, 0)));
        $("#LossOpenLine").text($.formatMoney($.round(info.open, 0)));
        $("#ManageMoney").text($.formatMoney(info.fee));
    }
};
win.prototype.showLimit = function (money) {
    var info = this.getLimit(money, this.getRiskMargin());
    if (info && info != "") {
        $("#noneLimit").hide();
        $("#limit").show().css("color", "#cd1818");
        $("#limitinfo").html(info).parent().css("line-height","25px");
    } else {
        $("#noneLimit").show();
        $("#limit").hide();
        $("#limitinfo").html("").parent().css("line-height", "50px");;
    }
};
//检测仓位限制
win.prototype.getLimit = function (money,rm) {
    if ($.isNumeric(money) && $.isNumeric(rm)) {
        if (rm == 5) {
            if (money <= 500000) {
                return "";
            } else if (money <= 1000000) {
                return "单股不超总操盘资金的50%";
            } else {
                return "单股不超总操盘资金的50%（创业板33%）";
            }
        } else if (rm == 10) {
            if (money <= 200000) {
                return "";
            } else if (money <= 1000000) {
                return "单股不超总操盘资金的50%";
            }
        } else if (rm == 15) {
            if (money <= 100000) {
                return "";
            } else {
                return "单股不超总操盘资金的50%";
            }
        }
    }
    return "";
};
win.prototype.getRiskMargin = function () {
    return parseInt($(":radio[name=RiskMargin]:checked").val());
};
win.prototype.checkTodayShowable = function (money) {
    var tday = $("#tday");
    if (money < 1000000) {
        if (tday.size() > 0) {
            tday.parent().show();
        }
    } else {
        if (tday.length > 0) {
            tday.parent().hide();
            $("#NextTrader").click();
        }
    }
};

//#region 天天赢
var daywin = new win();
daywin.rm15 = 300000;
daywin.rm10 = 1000000;
//#endregion

//#region 周周盈
var weekwin = new win();
weekwin.MathRiskMargin = function (money) {
    if ($.isInt(money)) {
        return { warning: money * 110 / 100, open: money * 107 / 100, principal: Math.floor(money / 5), fee: $.round(money * 1.2 / 1000, 4), interest: 1.2 };
    }
    return { warning: 0, open: 0, principal: 0, fee: 0, interest: 0 };;
};
weekwin.moneyChange = function (money) {
    if (!$.isInt(money)) return;
    var self = this;
    var riskMargin= self.MathRiskMargin(money);
    var $riskMargin = $("#RiskMargin");
    $riskMargin.text($.formatMoney(riskMargin.principal));
    $("#TotalTrade").text(money > 0 ? $.formatMoney($.round(money + riskMargin.principal, 0)) : "0");
    $("#LossWarLine").text($.formatMoney($.round(riskMargin.warning, 0)));
    $("#LossOpenLine").text($.formatMoney($.round(riskMargin.open, 0)));
    $("#ManageMoney").text($.formatMoney(riskMargin.fee));
};
weekwin.riskMarginChange = function () { };
weekwin.getLimit = function (money) {
    if ($.isNumeric(money)) {
        if (money <= 500000) {
            return "";
        } else if (money <= 1000000) {
            return "单股不超总操盘资金的50%";
        } else {
            return "单股不超总操盘资金的50%（创业板33%）";
        }
    }
    return "";
}
//#endregion

//#region 股票配资
var tradewin = new win();
//#endregion

//#region 注册

//#endregion